package Sort;

import API.StdRandom;
import API.Watch.StopWatch;

import java.util.Random;
import java.util.Scanner;

/**
 * Created by Lee on 2017/2/18.
 */
public class QuickSort {
    public static void sort(Comparable[] a) {
        StdRandom.shuffle(a);
        sort(a,0,a.length-1);
    }

    private static void sort(Comparable[] a, int lo, int hi) {
        if(hi <=lo){
            return;
        }
        int j = partition(a, lo, hi);
        sort(a,lo,j-1);
        sort(a, j+1, hi);
    }

    private static int partition(Comparable[] a, int lo, int hi) {
        int i=lo;
        int j=hi+1;
        Comparable v = a[lo];
        while (true) {
            while(less(a[++i],v)){
                if (i == hi) {
                    break;
                }
            }
            while(less(v,a[--j])){
                if(j==lo){
                    break;
                }
            }
            if(i>=j){
                break;
            }
            exch(a,i,j);
        }
        exch(a,lo,j);
        return j;
    }

    public static boolean less(Comparable v, Comparable w) {
        return v.compareTo(w) < 0;
    }

    private static void exch(Comparable[] a, int i, int j) {
        Comparable temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

    private static void show(Comparable[] comparable) {
        for (Comparable t : comparable) {
            System.out.println(t);
        }
        System.out.println();
    }

    /*
    * Check the order
    * */
    private static boolean isSorted(Comparable[] a) {
        for (int i=1;i<a.length;i++) {
            if(less(a[i],a[i-1]))
                return false;
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        StopWatch stopWatch = new StopWatch();
        Double[] a;
//        System.out.println(less(new Double(2.0),new Double(4.0)));
//        System.out.println(less(new Double(4.0),new Double(2.0)));
        int N = sc.nextInt();
        a = new Double[N];
        Random random = new Random(10);
        for(int i=0;i<N;i++) {
//            a[i] = sc.nextDouble();
            a[i] = random.nextDouble()*1000;
        }
        sort(a);
        show(a);
        System.out.println(stopWatch.elapsedMillis());
    }


}
